import { defineStore } from 'pinia'
import { ClassInfo } from '@/utils/types'

export const useBoardStore = defineStore('drawBoard', {
  state: () => ({
    showBoard: false,
    isSignalReady: false,
    isTiwReady: false, // 白板是否已经Ready
    classInfo: {
      // this.boardStore.setClassInfo({...}) //
      // sdkAppId: 1600049115, // number
      sdkAppId: 0, // number
      userId: sessionStorage.getItem('TIW_CLASSINFO_USERID'),
      userSig: sessionStorage.getItem('TIW_CLASSINFO_USERSIG'),
      classId: sessionStorage.getItem('TIW_CLASSINFO_CLASSID'),
      nickname: sessionStorage.getItem('TIW_CLASSINFO_NICKNAME'),
      imGroupId: sessionStorage.getItem('TIW_CLASSINFO_IMGROUPID'),
      dowmGradeTimes:
        sessionStorage.getItem('TIW_CLASSINFO_DOWNGRADETIMES') || 0
    },
    current: {
      toolType: 0,
      fileInfo: {},
      boardSetting: {}
    },
    boardSetting: {},
    rightBarShow: false
  }),
  actions: {
    setClassInfo(payload: ClassInfo) {
      sessionStorage.setItem('TIW_CLASSINFO_USERID', payload.userId)
      sessionStorage.setItem('TIW_CLASSINFO_USERSIG', payload.userSig)
      sessionStorage.setItem('TIW_CLASSINFO_CLASSID', payload.classId)
      sessionStorage.setItem('TIW_CLASSINFO_NICKNAME', payload.nickname)
      sessionStorage.setItem('TIW_CLASSINFO_IMGROUPID', payload.imGroupId || '')
      sessionStorage.setItem(
        'TIW_CLASSINFO_DOWNGRADETIMES',
        String(payload.dowmGradeTimes) || '0'
      )

      this.classInfo.sdkAppId = payload.sdkAppId
      this.classInfo.userId = payload.userId
      this.classInfo.classId = payload.classId
      this.classInfo.userSig = payload.userSig
      this.classInfo.nickname = payload.nickname
      this.classInfo.imGroupId = payload.imGroupId
      this.classInfo.dowmGradeTimes = payload.dowmGradeTimes
    },

    setShowBoard(value: boolean) {
      this.showBoard = value
    },

    setSignalReady(payload: boolean) {
      this.isSignalReady = payload
    },

    setTiwReady(payload: boolean) {
      this.isTiwReady = payload
    },

    setRightBarShow(payload: boolean) {
      this.rightBarShow = payload
    },

    setCurrentFile(payload = {}) {
      this.current.fileInfo = payload
    },

    updateBoardSetting(payload = {}) {
      const t = this.current.boardSetting
      this.current.boardSetting = {
        ...t,
        ...payload
      }
    }
  }
})
